# -*- coding: utf-8 -*-
import xbmc, xbmcgui, os, sys, time
from xbmcgui import Window

__scriptname__ = "蓝光播放"
__scriptID__ = "script.playbluraydisk"
__author__ = "simon shu"
__url__ = ""
__credits__ = ""
__version__ = "0.1.1"
#__addon__ = xbmcaddon.Addon(__scriptID__)

def main():

    hasmedia = os.popen("udisks --show-info /dev/sr0 |grep 'has media'").read()[31] == '1'

    #检测是否有光盘内容
    times = 10
    while (not hasmedia) and (times>0):
        times = times-1
        os.system("udisks --poll-for-media /dev/sr0")
        hasmedia = os.popen("udisks --show-info /dev/sr0 |grep 'has media'").read()[31] == '1'
        time.sleep(1)

    #无光盘
    if not hasmedia:
        dialog = xbmcgui.Dialog()
        ok = dialog.ok(__scriptname__, '无法播放：请插入光盘后再试')
        return

    ismounted = os.popen("udisks --show-info /dev/sr0 |grep 'is mounted'").read()[31] == '1'
    #已经加载
    if ismounted:
        mountpath = os.popen("udisks --show-info /dev/sr0 |grep 'mount paths'").read()[27:-1]
    else:
        ret = os.popen("udisks --mount /dev/sr0").read()
        if ret[:7] == "Mounted":
            mountpath = ret[47:].strip()
        else:
            dialog = xbmcgui.Dialog()
            ok = dialog.ok(__scriptname__, '无法播放：无法读取光盘')
            return

    if not os.path.exists(mountpath):
        dialog = xbmcgui.Dialog()
        ok = dialog.ok(__scriptname__, '无法播放：无效的光盘格式')
        return


    if os.path.exists(mountpath+"/BDMV/index.bdmv"):
        xbmc.Player().play(mountpath+"/BDMV/index.bdmv")
    else:
        xbmc.executebuiltin('PlayDVD')

if ( __name__ == "__main__" ):
    main()
